/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.mzk.oai.metadataRepository.cli;

import cz.mzk.oai.metadataRepository.metadata.MetadataFormat;
import cz.mzk.oai.metadataRepository.repository.collections.MetadataCollection;
import cz.mzk.oai.metadataRepository.repository.Repository;

/**
 *
 * @author Martin Řehánek (rehan at mzk.cz)
 */
public class ValidateMetadataAction extends Action {

    private final boolean printInvalidFiles;
    private MetadataCollection collectionToValidate;
    private boolean validateAllCollections;
    private MetadataFormat formatToValidate;

    public ValidateMetadataAction(Repository repository, String metadataFormat, String collectionName, boolean printInvalidFiles) throws Exception {
        super(repository);
        this.printInvalidFiles = printInvalidFiles;
        formatToValidate = MetadataFormat.parseString(metadataFormat);
        determineCollection(collectionName);
    }

    private void determineCollection(String collectionName) throws Exception {
        CollectionParser collectionParser = new CollectionParser(repository, collectionName);
        collectionToValidate = collectionParser.getCollection();
        validateAllCollections = collectionParser.allCollections();
    }

    @Override
    public void run() throws Exception {
        if (validateAllCollections) {
            repository.validateMetadata(formatToValidate, printInvalidFiles);
        } else {
            repository.validateMetadata(formatToValidate, collectionToValidate.getName(), printInvalidFiles);
        }
    }
}
